from urllib import request
import gzip
from lxml import etree
url = "https://hz.zu.fang.com/"
headers = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.9 Safari/537.36"
}
h = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.9 Safari/537.36",
    "cookie": "global_cookie=8ceymh3602gkj2y8dvtghw4pn10kav0ymjo; integratecover=1; city=hz; engine_source_cookie=baidu; sf_source=baidu; __utmc=147393320; Captcha=4F5A304E6B325654687162434A6C2F674A534B714B66762F2B4C6B536B32464B72792B695531422F594955696B796C526C6A325366576F32536B3953334A57507447464466446D3755646B3D; ASP.NET_SessionId=zywpfadqt54y1xderyzdwta1; g_sourcepage=zf_fy%5Exq_pc; Rent_StatLog=92704ccc-96c5-4e52-a4b8-24f16f4b9fec; __utma=147393320.757693015.1590927174.1590927174.1590930949.2; __utmz=147393320.1590930949.2.2.utmcsr=search.fang.com|utmccn=(referral)|utmcmd=referral|utmcct=/captcha-26a74f021f4d044b5e/redirect; unique_cookie=U_8ceymh3602gkj2y8dvtghw4pn10kav0ymjo*11; __utmt_t0=1; __utmt_t1=1; __utmt_t2=1; __utmb=147393320.9.10.1590930949"
}
req = request.Request(url=url,headers=headers)
print(req)
try:
    res = request.urlopen(req)
    s = res.read()
    print(s)
    result = gzip.decompress(s).decode("gb18030")
except Exception as e:
    print(e)
e = etree.HTML(result)
urls = e.xpath("//p[@class='title']/a/@href")
url_pre = "https://hz.zu.fang.com"
for u in urls:
    req_l = request.Request(url=url_pre+u,headers=h)
    res_l = request.urlopen(req_l)
    read_l = res_l.read().decode()
    print(read_l)
    e = etree.HTML(read_l)
    money = e.xpath("//div[@class='trl-item sty1']//text()")
    print(money)
    break